﻿@{
    ViewBag.Title = "Update";
    Layout = "~/Views/Shared/_AdminLayout.cshtml";
}


@section Header
{
    <script>
        function submit() {
            //编辑器对象
            var uEditor = UE.getEditor('txt_News');

            //验证输入
            if ($("#txt_Title").validatebox('isValid') == false) { return; }

            if (uEditor.hasContents() == false) {
                $.messager.alert('失败', '请输入内容', 'error');
                return;
            }


            //获取附件列表
            var attachmentString = "";
            //---------注意：这个地方有点恶心，冒号分割文件名和保存的的文件名，竖号分割每个附件，在实体类里面进行组装list
            $div = $('#AttarchmentDIV').children('div');
            $div.each(
                function () {
                    attachmentString += $(this).attr('fileName') + ':' + $(this).attr('saveName') + '|';
                }
            );


            //参数对象
            var news = {
                NewsID: $("#hid_ID").val(),
                NewsGroupName: $('#cbxGroupName').combobox('getValue'),
                IsTop: document.getElementById("cbx_IsTop").checked,
                TiTle: $("#txt_Title").val(),
                NewsContent: uEditor.getContent(),
                AttarchmentString: attachmentString
            };

            $.post("/News/UpdateNews/", news,
                    function (data, textStatus) {
                        if (data == 'success') { $.messager.alert('成功', '修改成功'); }
                        else { $.messager.alert('失败', data, 'error'); }
                    }
                  );
        }

        function goback() {
            window.location.href = "/News/Edit";
        }


        function removeAttarchment(divID) {
            $('#' + divID).remove();
        }

        //附件上传成功
        function onUploadSuccess(file, data, response) {
            eval("data=" + data);
            var fileId = data.FileID;
            $('#AttarchmentDIV').append('<div id="' + fileId + '" fileName = "' + file.name + '" saveName = "' + data.SaveName + '">' + file.name + '<a href = "#" onclick = "removeAttarchment(\'' + fileId + '\')">删除</a></div>');
        }
    </script>
}

<body>

    <div>
        @{ NewsInfo news = ViewData["News"] as NewsInfo;}
        @Html.Hidden("hid_ID", news.NewsID)
        标题:
        @Html.TextBox("txt_Title", news.Title, new { size = 50, @class = "easyui-validatebox", required = "true" })
        <br />
        新闻分组：
       <select id="cbxGroupName" class="easyui-combobox" name="state" style="width: 200px;">
           @{ 
               foreach (string item in ViewBag.NewsGroup)
               {
                   if (item == news.NewsGroupName)
                   {
               <option value="@item" selected>@item</option>  
                   }
                   else
                   {
               <option value="@item">@item</option>  
                   }
               }
           }
       </select>
        <br />
        是否置顶：  @Html.CheckBox("cbx_IsTop", news.IsTop)
        <br />
        @Html.UEditor("txt_News", news.NewsContent)
        <br />
        @Html.Uploader("file_uploader", "onUploadSuccess")
        <div id="AttarchmentDIV">
            @*参照Add页面附件 html 拼装逻辑，请先理解下Add*@
            @foreach (var item in news.AttachmentList)
            {
                <div id='@item.FileID' fileName = '@item.FileName' saveName = '@item.SaveName'>
                    @item.FileName<a href="#" onclick="removeAttarchment('@item.FileID')">删除</a>
                </div>
            }
        </div>
        <p>
            <input type="button" value="修改" onclick="submit()" />
            <input type="button" value="返回" onclick="goback()" />
        </p>
    </div>
</body>
